// 解析完整日期
export function timestampToTime(timestamp, state) {
  let date = new Date(timestamp)
  let Y = date.getFullYear();
  let M = date.getMonth() + 1;
  if (M < 10) {
    M = "0" + M
  }
  let D = date.getDate();
  if (D < 10) {
    D = "0" + D
  }
  let h = date.getHours();
  if (h < 10) {
    h = "0" + h
  }
  let m = date.getMinutes();
  if (m < 10) {
    m = "0" + m
  }
  let s = date.getSeconds();
  if (s < 10) {
    s = "0" + s
  }
  return state ? `${Y}-${M}-${D} ${h}:${m}:${s}` : `${Y}-${M}-${D}`;
}
// 获取当前完整日期
export function getCurrentTime(state) {
  let date = new Date()
  let Y = date.getFullYear();
  let M = date.getMonth() + 1;
  if (M < 10) {
    M = "0" + M
  }
  let D = date.getDate();
  if (D < 10) {
    D = "0" + D
  }
  let h = date.getHours();
  if (h < 10) {
    h = "0" + h
  }
  let m = date.getMinutes();
  if (m < 10) {
    m = "0" + m
  }
  let s = date.getSeconds();
  if (s < 10) {
    s = "0" + s
  }
  return state ? `${Y}-${M}-${D} ${h}:${m}:${s}` : `${Y}-${M}-${D}`;
}
// 获取当前年月
export function getCurrentTimeYM() {
  let date = new Date()
  let Y = date.getFullYear();
  let M = date.getMonth() + 1;
  if (M < 10) {
    M = "0" + M
  }
  let D = date.getDate();
  if (D < 10) {
    D = "0" + D
  }
  let h = date.getHours();
  if (h < 10) {
    h = "0" + h
  }
  let m = date.getMinutes();
  if (m < 10) {
    m = "0" + m
  }
  let s = date.getSeconds();
  if (s < 10) {
    s = "0" + s
  }
  return `${Y}-${M}`
}
// 相差天数
export function getDaysBetween(dateString1, dateString2) {
  var startDate = Date.parse(dateString1);
  var endDate = Date.parse(dateString2);
  var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
  // alert(days);
  return days;
}

// 获取上一月
export function getLastMonth() {
  var year, lastMonth;
  var date = new Date();
  var nowYear = date.getFullYear();   //当前年：四位数字
  var nowMonth = date.getMonth();     //当前月：0-11
  if (nowMonth == 0) {   //如果是0，则说明是1月份，上一个月就是去年的12月
    year = nowYear - 1;
    lastMonth = 12;
  } else { //不是1月份，年份为当前年，月份本来是要减1的，但是由于`getMonth()`的月份本身就是少了1的，所以月份不用变。
    year = nowYear;
    lastMonth = nowMonth;
  }

  lastMonth = lastMonth < 10 ? ('0' + lastMonth) : lastMonth;   //月份格式化：月份小于10则追加个0

  let lastYearMonth = year + '-' + lastMonth;

  return lastYearMonth;
}

// 获取七天前日期
export function getSevenMonth() {
  var _date = new Date(); //获取今天日期
  _date.setDate(_date.getDate() - 7);//日期回到七天前
  var year = _date.getFullYear();
  var month = _date.getMonth() + 1;
  var day = _date.getDate();
  if (month < 10) {
    month = '0' + month;
  };
  if (day < 10) {
    day = '0' + day;
  };

  var dateTemp = year + '-' + month + '-' + day;
  _date.setDate(_date.getDate() + 7);//日期重置
  return dateTemp;
}